﻿using HPIT.MemberPoint.Common;
using HPIT_LiuzuPoint.Entity.POCOModel;
using HPIT_LiuzuPoint.Entity.ViewModel;
using MPMS.DTOModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MPMS.DTOModel.OperateResult;

namespace HPIT_LiuzuPoint.Business
{
    /// <summary>
    /// 礼品的业务逻辑
    /// 实现礼品增删改查
    /// </summary>
    public class GiftLevelService : BaseService<ExchangeGifts>
    {
        /// <summary>
        /// 对礼品列表进行分页
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult GetPagedGiftList(GiftSearchViewModel viewModel)
        {

            int rowCount = 0;
            //利用lambda表达式树返回一个IQueryable的皆苦
            var query = PredicateExtensions.True<ExchangeGifts>();

            if (!string.IsNullOrWhiteSpace(viewModel.GiftCode))
                query = query.And(m => m.EG_GiftCode == viewModel.GiftCode);
            if (!string.IsNullOrWhiteSpace(viewModel.GiftName))
                query = query.And(m => m.EG_GiftName.Contains(viewModel.GiftName));
            if (viewModel.GiftLevel > 0)
                query = query.And(m => m.EG_ID == viewModel.GiftLevel);

            //调用BaseService封装好的分页方法
            var list = GetPageList(viewModel.Page, viewModel.Rows, ref rowCount, query, m => m.EG_ID, false);

            var data = new EasyUIDataGrid() { total = rowCount, rows = list };
            return new OperateResult(ResultStatus.Success, "", data);
        }

        /// <summary>
        /// 添加礼品的业务
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult AddGift(GiftEditViewModel viewModel)
        {

            var exchange = new ExchangeGifts()
            {
                EG_ExchangeNum = viewModel.EG_ExchangeNum,
                EG_GiftCode = viewModel.EG_GiftCode,
                EG_Number = viewModel.EG_Number,
                EG_Photo = viewModel.EG_Photo,
                EG_Point = viewModel.EG_Point,
                EG_GiftName = viewModel.EG_GiftName,
                EG_Remark = viewModel.EG_Remark,
                EG_ID = viewModel.EG_ID
            };

            if (Add(exchange))
            {
                return new OperateResult(ResultStatus.Success, "添加成功");
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "网络问题，请稍后重试");
            }

        }

        public OperateResult GetGift(int id)
        {
            var model = Find(e => e.EG_ID == id);
            if (model != null)
            {
                var viewModel = new GiftEditViewModel()
                {
                    EG_ID = model.EG_ID,
                    EG_ExchangeNum = model.EG_ExchangeNum,
                    EG_GiftCode = model.EG_GiftCode,
                    EG_GiftName = model.EG_GiftName,
                    EG_Number = model.EG_Number,
                    EG_Photo = model.EG_Photo,
                    EG_Point = model.EG_Point,
                    EG_Remark = model.EG_Remark
                };
                return new OperateResult(ResultStatus.Success, "", viewModel);
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "该礼品未找到");
            }
        }

        /// <summary>
        /// 更新礼品信息的业务
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult EditGetGift(GiftEditViewModel viewModel)
        {
            //调用父类里面的公共查询
            var model = Find(e => e.EG_ID == viewModel.EG_ID);
            if (model!=null)
            {
                model.EG_ID = viewModel.EG_ID;
                model.EG_GiftName = viewModel.EG_GiftName;
                model.EG_GiftCode = viewModel.EG_GiftCode;
                model.EG_ExchangeNum = viewModel.EG_ExchangeNum;
                model.EG_Number = viewModel.EG_Number;
                model.EG_Photo = viewModel.EG_Photo;
                model.EG_Point = viewModel.EG_Point;
                model.EG_Remark = viewModel.EG_Remark;

                if (Update(model))
                {
                    return new OperateResult(ResultStatus.Success, "更新成功");
                }
                else
                {
                    return new OperateResult(ResultStatus.Error, "网络繁忙，请稍后重试^^^");
                }
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "该学员不存在");
            }
        }

        public OperateResult DeleteGift(int id)
        {
           var model= Find(e => e.EG_ID == id);
            if (model!=null)
            {
                if (Delete(model))
                {
                    return new OperateResult(ResultStatus.Success, "删除成功");
                }
                else
                {
                    return new OperateResult(ResultStatus.Error, "网络繁忙，请稍后重试");
                }
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "该学员已删除，请稍后重试");
            }
        }
    }
}

